<?php

switch ($this->Param['option']) {
    case 'city':
        $this->LoadHelper('CityHelper');
        $CityHelper = new CityHelper();
        $data = $CityHelper->regroupCity($this->Param['bank_id']);
        echo json_encode($data);
        break;
    case 'area':
        $this->LoadHelper('CityHelper');
        $CityHelper = new CityHelper();
        $city = empty($this->Param['city']) ? ErrorMsg::Debug('参数错误') : $this->Param['city'];
        $data = $CityHelper->getArea($city,$this->Param['bank_id']);
        echo json_encode($data);
        break;
    case 'ocr_idcard':
        $this->LoadResurces('aliyun/class.idcard');
        $idcardOcr = new Idcard();
        $data = $idcardOcr->getCardInfo('attach/201701/20/1.jpg', 'back');
        print_r($data);
        break;
    case 'mobile_area':
        $mobile = empty($this->Param['mobile']) ? ErrorMsg::Debug('参数错误') : $this->Param['mobile'];
        $url = 'http://api.k780.com/?app=phone.get&phone=' . $mobile . '&appkey=21006&sign=07cfecee1318119f865e6da88fc66c86&format=json';
        $content = file_get_contents($url);
        $configData = json_decode($content, true);
//        $mobile = array(
//            'province'=>'',
//            'city'=>str_replace(',','',str_replace('中国,','',$configData['result']['att'])),
//            'supplier'=>str_replace('中国','',$configData['result']['operators'])
//        );
        $area = str_replace(',', '', str_replace('中国,', '', $configData['result']['att'])) . str_replace('中国', '', $configData['result']['operators']);
        ErrorMsg::Debug($area, true);
        break;
    case 'dpinfo': //部门
        $dp_id = empty($this->Param['dp_id']) ? 0 : $this->Param['dp_id'];
        $this->LoadHelper('DepartmentHelper');
        $DepartmentHelper = new DepartmentHelper();

        $this->LoadHelper('BankHelper');
        $BankHelper = new BankHelper();

        $where = array(
            '`dp_status` = ?' => 'ok'
        );
        $data = $DepartmentHelper->dpAll($where);
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]['value'] = 'dp_' . $v['dp_id'];
                if (!empty($v['dp_bank'])) {
                    $bank = $BankHelper->bankAll(array('`bk_status` = ?' => 'ok', '`bk_id` IN (?)' => explode(',', $v['dp_bank'])));
                    $data[$k]['bank_list'] = $bank;
                } else {
                    $data[$k]['bank_list'] = array();
                }
                if ($dp_id == $v['dp_id']) {
                    $data[$k]['select'] = true;
                } else {
                    $data[$k]['select'] = false;
                }
            }
        }
        echo json_encode($data);
        break;
    case 'region': //网点
        $list = empty($this->Param['list']) ? 'all' : $this->Param['list'];

        $this->LoadHelper('RegionHelper');
        $RegionHelper = new RegionHelper();

        $this->LoadHelper('BranchHelper');
        $BranchHelper = new BranchHelper();

        $this->LoadHelper('BankHelper');
        $BankHelper = new BankHelper();

        $where = array(
            '`rg_status` = ?' => 'ok'
        );
        $region = $RegionHelper->regionAll($where);
        if (!empty($region)) {
            foreach ($region as $k => $v) {
                $region[$k]['value'] = 'rg_' . $v['rg_id'];
                if ($list == 'all') {
                    $branch = $BranchHelper->branchAll(array(
                        '`br_status` = ?' => 'ok',
                        '`br_rgid` = ?' => $v['rg_id']
                    ));
                    if (!empty($branch)) {
                        foreach ($branch as $key => $val) {
                            $branch[$key]['value'] = 'dot_' . $val['br_id'];
                            $bank = $BankHelper->bankAll(array('`bk_status` = ?' => 'ok', '`bk_id` IN (?)' => explode(',', $val['br_bank'])));
                            $branch[$key]['bank_list'] = $bank;
                        }
                    }
                    $region[$k]['branch'] = $branch;
                }
            }
        }
        echo json_encode($region);
        break;
    case 'get_salesman'://获取业务网点人员
        $rg_id = $this->Param['rg_id'];

        $this->LoadHelper('BranchHelper');
        $BranchHelper = new BranchHelper();

        $this->LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();

        $where = array('`u_status` = ?' => 'ok');
        if ($rg_id > 0) {
            /*$br_id = array($rg_id);
            $branch_all = $BranchHelper->branchAll(array('`br_rgid` = ?' => $rg_id, '`br_status` = ?' => 'ok'));
            if (!empty($branch_all))
                foreach ($branch_all as $k => $v) {
                    $br_id[] = $v['br_id'];
                }
             * 
             */
            $where['`u_dp_type` = ?'] = 'rg';
            $where['`u_dp_id` = ?'] = $rg_id;
        }else{
            $where['`u_id` = ?'] = 0;
        }
        $member_all = $MemberHelper->getMemberAll($where);
        $data = array();
        if (!empty($member_all))
            foreach ($member_all as $k => $v) {
                $data[] = array(
                    'u_id' => $v['u_id'],
                    'u_realname' => $v['u_realname'],
                    'u_code' => $v['u_code']
                );
            }
        echo json_encode($data);
        break;
    case 'role'://角色列表
        $this->LoadHelper('RoleHelper');
        $RoleHelper = new RoleHelper();
        $where['`r_status` = ?'] = 'ok';
        $role = $RoleHelper->roleAll($where);
        echo json_encode($role);
        break;
    case 'dictionary'://字典数据获取
        $type = empty($this->Param['type']) ? '学历' : $this->Param['type'];
        if($type == '产品类型'){
            $bk_id = empty($this->Param['bank_id']) ? ErrorMsg::Debug('银行值错误') : $this->Param['bank_id'];
            $ot_loan_year = empty($this->Param['ot_loan_year']) ? ErrorMsg::Debug('贷款年限值错误') : $this->Param['ot_loan_year'];
            $this->LoadHelper('BankHelper');
            $BankHelper = new BankHelper();
            $rt_all = $BankHelper->rateAll(array('`rt_status` = ?' => 'ok', '`rt_bank_id` = ?' => $bk_id, '`rt_ylimit` = ?' => $ot_loan_year), array('rt_type'), array('rt_type'));
            $arr = array();
            if(!empty($rt_all)) foreach($rt_all as $k => $v){
               $arr[] = array(
                   'd_id' => $v['rt_id'],
                   'd_fid' => 0,
                   'd_text' => $v['rt_type']
               );
               $data = json_encode($arr);
            }
        }else{
            $url = 'http://dictapi.app.qiche100.com/public/index/option/dict?type=' . $type;
            $data = file_get_contents($url);
        }
        //$data = json_decode($data, TRUE);
        //echo json_encode($data);
        echo $data;
        break;
    case 'jobnum'://生成工号
        $dp_code = empty($this->Param['dp_code']) ? ErrorMsg::Debug('参数错误') : $this->Param['dp_code'];
        $dp_code = explode('_', $dp_code);
        $this->LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();
        $dpinfo = $MemberHelper->getDpinfo(array(
            'u_dp_type' => $dp_code[0],
            'u_dp_id' => $dp_code[1]
        ));
        $job_no = $MemberHelper->getMemberNo($dpinfo['dpcode']);
        echo $job_no;
        break;
    case 'branchnum': //生成网点编号
        $province = empty($this->Param['province']) ? ErrorMsg::Debug('参数错误') : $this->Param['province'];
        $city = empty($this->Param['city']) ? ErrorMsg::Debug('参数错误') : $this->Param['city'];
        $this->LoadHelper('BranchHelper');
        $BranchHelper = new BranchHelper;
        $branch_no = $BranchHelper->getBranchNo($province,$city);
        echo $branch_no;
        break;
    case 'sendsms':
        $mobile = empty($this->Param['mobile']) ? ErrorMsg::Debug('请填写手机号') : $this->Param['mobile'];
        if (!Validator::isMobile($mobile)) {
            ErrorMsg::Debug('手机号错误');
        }
        $check_phone = $this->LoadModel('Public')->getOneRow('member','*',array('`u_mobile` =?' =>$mobile));
        if(empty($check_phone)){
            ErrorMsg::Debug('该手机号未在本系统注册');
        }
        $this->LoadHelper('SmsHelper');
        $SmsHelper = new SmsHelper();
        $this->LoadResurces('sms/class.sms');
        $sms = new Sms();
        $rnd = StringCode::RandomCode(4, 'num');
        $do = $sms->sendsms($mobile, '您的验证码是：' . $rnd);
        if ($do == '0') {
            $SmsHelper->verifySave(array(
                'sms_mobile' => $mobile,
                'sms_code' => $rnd,
                'sms_dateline' => NOW_TIME,
                'sms_ip' => C_IP
            ));
            ErrorMsg::Debug('发送成功', TRUE);
        } else {
            ErrorMsg::Debug('发送失败');
        }

        break;
    case 'brmember'://当前用户同网点所有用户
        $this->LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();

        $this->LoadHelper('BranchHelper');
        $BranchHelper = new BranchHelper();
        $member = $MemberHelper->getMember($this->UserConfig['Id']);
        $where['`u_status` = ?'] = 'ok';
        if ($member['u_dp_type'] == 'rg') {
            $branch = $BranchHelper->branchRow(array('`br_status` = ?' => 'ok', '`br_rgid` = ?' => $member['u_dp_id']));
            $br_id = array(0);
            if (!empty($branch))
                foreach ($branch as $k => $v) {
                    $br_id[] = $v['br_id'];
                }
            $where['`u_dp_type` = ?'] = 'dot';
            $where['`u_dp_id` IN(?)'] = $br_id;
        } else {
            $where['`u_dp_type` = ?'] = $member['u_dp_type'];
            $where['`u_dp_id` = ?'] = $member['u_dp_id'];
        }
        $data = $MemberHelper->getMemberAll($where);
        if (!empty($data))
            foreach ($data as $k => $v) {
                $json_data[] = array(
                    'u_id' => $v['u_id'],
                    'u_realname' => $v['u_realname']
                );
            }
        echo json_encode($json_data);
        break;
    case 'getdealers': //所有经销商
        $this->LoadHelper('DealersHelper');
        $DealersHelper = new DealersHelper();
        
        $this->LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();
        
        $this->LoadHelper('BranchHelper');
        $BranchHelper = new BranchHelper();
        
        $member = $MemberHelper->getMemberRow($this->UserConfig['Id']);
        
        $where = array(
            '`dl_status` = ?' => 'ok'
        );
        switch ($member['u_dp_type']){
            case 'dot':
                $where['`dl_brid` = ?'] = $member['u_dp_id'];
                break;
            case 'rg':
                $branch = $BranchHelper->branchAll(array('`br_rgid` = ?' => $member['u_dp_id'], '`br_status` = ?' => 'ok'));
                $branch_id = array(0);
                if(!empty($branch)) foreach($branch as $k => $v){
                    $branch_id[] = $v['br_id'];
                }
                $where['`dl_brid` IN (?)'] = $branch_id;
                break;
        }

        $data = $DealersHelper->dealersAll($where);
        echo json_encode($data);
        break;
    case 'plate':
        $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('订单号错误') : $this->Param['oid'];
        
        break;
    case 'bankpro': //获取指定银行当前的利率信息
        $this->LoadHelper('BankHelper');
        $BankHelper = new BankHelper();
        $bank_id = empty($this->Param['o_bank_id']) ? ErrorMsg::Debug('银行ID值错误') : $this->Param['o_bank_id'];
        $loan_year = empty($this->Param['ot_loan_year']) ? ErrorMsg::Debug('贷款年限值错误') : $this->Param['ot_loan_year'];
        $loan_type = empty($this->Param['ot_loan_type']) ? ErrorMsg::Debug('产品类型错误') : $this->Param['ot_loan_type'];
        $province = empty($this->Param['ot_reg_province']) ? ErrorMsg::Debug('车辆上牌地不能为空') : $this->Param['ot_reg_province'];
        $car_tp = empty($this->Param['ot_car_pose']) ? ErrorMsg::Debug('车型构成不能为空') : $this->Param['ot_car_pose'];
        $params = array(
            'o_bank_id' => $bank_id,
            'ot_loan_type' => $loan_type,
            'ot_loan_year' => $loan_year,
            'ot_reg_province' => $province,
            'ot_car_pose' => $car_tp,
            'oid' => $this->Param['oid']
        );
        $result = $BankHelper->rateBack($params);
        ErrorMsg::Debug(array('code' => $result['data']['code'], 'rt_id' => $result['data']['rt_id'], 'company_rate' => $result['data']['company_rate'], 'rate' => $result['data']['rate']), TRUE);
        break;
    case 'untreated': //用户未处理数据数字
        $this->LoadHelper('OrderHelper');
        $OrderHelper = new OrderHelper();
        
        $this->LoadHelper('MemberHelper');
        $MemberHelper = new MemberHelper();
        
        $this->LoadHelper('StretchHelper');
        $StretchHelper = new StretchHelper();
        
        $this->LoadHelper('SettingHelper');
        $SettingHelper =  new SettingHelper();

        $this->LoadHelper('EstimateHelper');
        $EstimateHelper = new EstimateHelper();
        
        $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
        
        $data = array();
        $status = empty($this->Param['status']) ? ErrorMsg::Debug('参数错误') : $this->Param['status'];
        $status = explode(',', $status);
        if(in_array('待通融', $status)){
            $where = array();
            $where['`sr_result` = ?'] = '申请中';
            $stretch = $StretchHelper->signList($where, 1, 1, $this->Param);
            $data['dtr'] = empty($stretch['count']) ? 0 : $stretch['count'];
        }
        if(in_array('修改待审核', $status)){
            $where = array();
            $where['`o_status` != ?'] = '作废';
            if($member_info['u_dp_type'] == 'dot'){
                if($member_info['u_scope'] == '用户级'){
                    $where['`o_branch_type` = "dot" AND `o_uid` = ?'] = $member_info['u_id'];
                }
                if($member_info['u_scope'] == '部门级'){
                    $where['`o_branch_type` = "dot" AND `o_branch_id` = ?'] = $member_info['u_dp_id'];
                }
            }

            if($member_info['u_role'] == 8) {//电审
                //$where = array(
                    //'`up_type` = ?' => '电审',
                   // '`o_final_perch` = ?' => $member_info['u_id']
                //);
                $where[o_final_perch] = $member_info['u_id'];
            }

            if($member_info['u_role'] == 7){//资审
                //$where = array(
                    //'`up_type` = ?' => '资审',
                    //'`o_perch` = ?' => $member_info['u_id']
                //);
                $where[o_perch] = $member_info['u_id'];
            }
            $stretch = $this->LoadModel('Public')->getRowCount('order_uping', $where);
            $data['ddxgdsh'] = empty($stretch) ? 0 : $stretch;
        }
        if(in_array('初审提交', $status)){
            $where = array(
                '`o_status` IN(?)' => array('初审提交', '审核中'),
                '`o_trial_status` IN(?)' => array('审核中', '未审核'),
            );
            $where['`ot_type` = ?'] = '资料审核';
            if ($member_info['u_role'] == 7) {//资料审核
                $where['`o_perch` IN (?)'] = array($member_info['u_id'], 0);
            }
            $order = $OrderHelper->orderList($where, 1, 1, $this->Param, $member_info, 'order_split');
            $data['dzlsh'] = empty($order['count']) ? 0 : $order['count'];
        }
        if(in_array('初审通过', $status)){
            $where = array(
                '`o_status` IN(?)' => array('初审提交', '审核中'),
                '`o_layer_status` IN(?)' => array('审核中', '未审核'),
            );
            $where['`ot_type` = ?'] = '层级审核';
            //根据用户审核层级锁定审核范围的订单
            //if($member_info['u_auditlevel'] > 0){
            //    $audit_level = $SettingHelper->auditLevel($member_info['u_auditlevel']);
            //    if(!empty($audit_level['last'])){
            //        $where['`o_layer_aucode` = ?'] = $audit_level['last']['au_code']; 
            //    }else{
            //        $where['`o_layer_aucode` = ?'] = '';
            //    }
            //}

            //if($member_info['u_role'] == 8){//电审员
            //    $where['`o_final_perch` IN (?)'] = array($this->UserConfig['Id'], 0);
            //}
            if($member_info['u_auditlevel'] > 0){
                if ($member_info['u_role'] == 8) {//电审员
                    $where['`o_final_perch` IN (?)'] = array($member_info['u_id'], 0);
                }else{
                    $where['`o_final_perch` =?'] = $member_info['u_id'];
                }
            }
            
            $order = $OrderHelper->orderList($where, 1, 1, $this->Param, $member_info, 'order_split');
            $data['dzs'] = empty($order['count']) ? 0 : $order['count'];
        }
        if(in_array('待审核',$status)){
            $where = array(
                '`o_status` IN(?)' => array('初审提交', '审核中'),
            );
            $order = $OrderHelper->orderList($where, 1, 1, $this->Param, $member_info, 'order');
            $data['dsh'] = empty($order['count']) ? 0 : $order['count'];
        }
        if(in_array('待视频审核',$status)){
            $where = array(
                '`o_status` IN(?)' => array('初审提交', '审核中'),
                '`o_videoaudit_status` IN(?)' => array('审核中', '未审核'),
            );
            if ($member_info['u_role'] == 10) {//视频审核
                $where['`o_videoaudit_perch` IN (?)'] = array($member_info['u_id'], 0);
            }
            $order = $OrderHelper->orderList($where, 1, 1, $this->Param, $member_info, 'order');
            $data['dsps'] = empty($order['count']) ? 0 : $order['count'];
        }
        if(in_array('终审提交', $status)){
            $where = array(
                '`o_status` = ?' => '终审通过',
                '`o_pre_mode` = ?' => 3,
                '`o_trial_status` = ?' => '已通过',
                '`o_layer_status` = ?' => '已通过',
                '`o_review_status` = ?' => '未复核'
            );
            $order = $OrderHelper->orderList($where, 1, 1, $this->Param, $member_info);
            $data['dshjzlfh'] = empty($order['count']) ? 0 : $order['count'];
        }
        if(in_array('待支付', $status)){
            $this->LoadResurces('oldapi/class.carloan');
            $carloan = new Carloan();
            switch($member_info['u_scope']){
                case '用户级':
                    $dpcode = '-1';
                    break;
                case '部门级':
                    $dpcode = $member_info['u_dpcode'];
                    break;
                default :
                    $dpcode = '';
            }                
            $result = $carloan->getPayorder($dpcode);
            $result = json_decode($result, TRUE);
            $data['dzf'] = empty($result['total']) ? 0 : $result['total'];
        }
        if(in_array('征信提交', $status)){
            $where = array();
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $where['`o_status` = ?'] = '征信提交';
            $where['`o_branch` NOT IN(?)'] = array('技术部','系统测试业务部');
            $order = $OrderHelper->orderList($where, 1, 1, $this->Param, $member_info);
            $data['dzxcx'] = empty($order['count']) ? 0 : $order['count']; 
        }
        if(in_array('征信查询', $status)){
            $where = array();
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $where['`o_status` = ?'] = '征信查询';
            $order = $OrderHelper->orderList($where, 1, 1, $this->Param, $member_info);
            $data['dcsdtj'] = empty($order['count']) ? 0 : $order['count'];   
        }
        if(in_array('待评估', $status)){
            $where = array();
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $where = array(
                '`et_status` NOT IN (?)'=> array('作废','退回','已使用','已评估')
            );
            $estRow = $EstimateHelper->estimateList($where, 1, 1, $this->Param,$member_info);
            $data['ddpg'] = empty($estRow['count']) ? 0 : $estRow['count'];
        }

        echo json_encode($data);
        break;
    case 'carmodel':
        $model = $this->LoadModel('Public');
        $car_arr = $model->getList('car_series','*',array('`cs_id` >?' =>0));
        foreach($car_arr as $k =>$v){
            $data[$k]['d_text'] = $v['cs_car_text'];
            $data[$k]['d_id'] = $v['cs_id'];
        }
        echo json_encode($data);
        
        break;
    case 'carfullmodel':
        $model = $this->LoadModel('Public');
        if(isset($this->Param['cs_id'])){
            $model_arr = $model->getList('car_model','*',array('`series_id` =?' =>$this->Param['cs_id']));
        }else{
            $model_msg = $model->getOneRow('car_series','*',array('`cs_car_text` =?' =>$this->Param['type']));
            $model_arr = $model->getList('car_model','*',array('`series_id` =?' =>$model_msg['cs_id']));
        }
        foreach($model_arr as $k =>$v){
            $data[$k]['d_text'] = $v['model_name'];
        }
        echo json_encode($data);
        break;
    case 'carmsg':
        $model = $this->LoadModel('Public');
        $data = $model->getOneRow('car_model','*',array('`model_name` =?' =>$this->Param['car_fullmodel']));
        if(!empty($data)){
            $car_series_row = $model->getOneRow('car_series','*',array('`cs_id` =?' =>$data['series_id']));
            $data['type'] = $car_series_row['cs_car_type'].'车';
            $data['brand'] = $car_series_row['cs_car_brand'];
            $result['success'] = true;
            $result['data'] = $data;
            ErrorMsg::Debug($result['data'], TRUE);
        }else{
            $result['success'] = false;
            ErrorMsg::Debug('获取失败');
        }
        break;
    default;
}